package vcp.client.GUI.mvc.view.custom;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;

import vcp.client.GUI.mvc.view.addons.JCalendarWrapper;
import vcp.client.utility.Common;
import vcp.client.utility.UtilityHelper;

import com.toedter.calendar.JCalendar;

/**
 * 
 * @author VCP team
 *         <p>
 *         This is customized JButton which looks like calendar icon this class
 *         binds itself to an instance of JTextField to present selected date
 * 
 *         this class listen to it's own button pressing which triggers pop-up
 *         {@link java.awt.Window} containing
 *         {@link com.toedter.calendar.JCalendar}
 *         </p>
 * 
 * @see JCalendar
 * @see Window
 */
public class CustomCalendarIconButton extends JButton implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3697372408594470722L;

	// ****************************************************************************************************************************
	// Setter-Getter methods
	// ****************************************************************************************************************************
	/** This variable is a reference to binding text field */
	private final JTextField bindingTextField;

	public JTextField getBindingTextField ()
	{
		return this.bindingTextField;
	}

	/** This variable is a reference to calendar wrapper */
	private JCalendarWrapper calendarWrapper;

	public CustomCalendarIconButton ( JTextField bindingTextField )
	{
		super();
		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CALENDAR, 27, 20);
		if ( icon != null )
		{
			this.setIcon(icon);
		}
		this.setPreferredSize(new Dimension(30, 25));
		this.bindingTextField = bindingTextField;
		this.addActionListener(this);
	}

	// ****************************************************************************************************************************
	// Public methods
	// ****************************************************************************************************************************

	/**
	 * clear instance
	 */
	public void dispose ()
	{
		this.calendarWrapper.dispose();
		this.calendarWrapper = null;
	}

	// ****************************************************************************************************************************
	// ActionListener methods
	// ****************************************************************************************************************************
	@Override
	public void actionPerformed ( ActionEvent e )
	{
		this.calendarWrapper = new JCalendarWrapper(this);
		this.calendarWrapper.setVisible(true);
	}

}
